//
//  LXMarkViewController.m
//  cat
//
//  Created by imera on 16/3/25.
//  Copyright © 2016年 LX. All rights reserved.
//  收藏列表

#import "LXMarkViewController.h"
#import "LXNavBarView.h"
#import "LXSearchTableViewCell.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "LXVideoShowViewController.h"
#import "LXLoginViewController.h"

@interface LXMarkViewController() <LXNavBarViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *videoArr;

@property (nonatomic, weak) UITableView *tableView;

@end

@implementation LXMarkViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getVideos];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    self.view.backgroundColor = MainBackgroundColor;
    
    [self getVideos];
 
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"收藏记录";
    navBarView.myDelegate = self;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];
    
    UITableView *tableView = [[UITableView alloc]init];
    tableView.frame = CGRectMake(0, 20 + LXNavBarHeight, self.view.frame.size.width, self.view.frame.size.height - 20 - LXNavBarHeight);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
}

#pragma mark - 获取收藏列表
- (void)getVideos
{
    if (self.user != nil) {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/getCollection";
        
        [mgr POST:url parameters:@{@"userid":@(self.user.ID)} success:^(AFHTTPRequestOperation *operation, id response) {
            NSArray *arr = response;
            if ([response isKindOfClass:[NSArray class]]) {
                NSMutableArray *videoArr = [NSMutableArray array];
                for (NSDictionary *dict in arr) {
                    LXVideo *video = [LXVideo initWithDict:dict];
                    [videoArr addObject:video];
                }
                self.videoArr = videoArr;
                [self.tableView reloadData];
            }else{
                UILabel *label = [[UILabel alloc]init];
                label.text = @"暂无收藏的视频";
                label.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:label];
                
                CGFloat labelW = 200;
                CGFloat labelH = 100;
                CGFloat labelX = (self.view.frame.size.width - labelW) / 2;
                CGFloat labelY = (self.view.frame.size.height - labelH) / 2;
                label.frame = CGRectMake(labelX, labelY, labelW, labelH);
            }
            
        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            [MBProgressHUD showError:@"您的网络开小差了..."];
        }];
    }
}

#pragma mark - 导航栏的代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (LXSearchTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    LXSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LXSearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.video = self.videoArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXVideoShowViewController *videoShow = [[LXVideoShowViewController alloc]init];
    videoShow.video = self.videoArr[indexPath.row];
    [self.navigationController pushViewController:videoShow animated:YES];
}
@end
